<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth;
header('Content-Type:text/html;charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
class YjfkController extends Controller
{
    public function messages($table){
        if($table == 'add'){
            return [
                'jbr.required'  => '姓名不能为空',
                'lxdh.required' => '联系电话不能为空',
                'jbnr.required' => '意见不能为空',
            ];
        }
        if($table == 'edit'){
            return [
                'cldz.required' => '县市区、派出所不能为空',
                'cldw.required'  => '处理单位不能为空',
                'clry.required' => '经办人不能为空',
                'cldh.required' => '联系电话不能为空',
                'clqk.required' => '核查结果不能为空',
            ];
        }
    }

	  public function index(Request $request){
	
		if($request->rows) {

		$userid['unit_id'] = Auth::guard('admin')->user()->unit_id;
		$res = DB::table('yjfk')->where($userid)->orderBy('yj_id', 'DESC')->paginate($request->rows);
		$list = $res->items();

	    foreach ($list as $k=>$v){

               if($v->police_cl == 0){
                     $list[$k]->police_cl = '未处理';
                }else{
                     $list[$k]->police_cl = '已处理';
                }
	    }
			$result['total'] = $res->total();
	        $result['rows'] = $list;
	        echo json_encode($result);

		}else{

		    return view('admin.yjfk.index');
		}

    }

    

    public function add(Request $request){
     	if($request->isMethod('post')){
            $this->validate($request, [
                'jbr' => 'required',
                'lxdh' => 'required',
                'jbnr' => 'required',
            ],$this->messages('add'));
     		$input = $request->all();
            $data = Input::except('_token');

            if($request['pic']){
				$images = $request['pic'];
                $filedir = "upload/xs/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(0,10000).".".$extension;
                $images->move($filedir,$newImagesName);

                $data = array(
                    'jbsj' => date('Y-m-d H:i:s',time()),
                    'jbr' => $input['jbr'],
                    'lxdh' => $input['lxdh'],
                    
                    'jbnr' => $input['jbnr'],
                    'unit_id' =>  Auth::guard('admin')->user()->unit_id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'jbwj' => $filedir.$newImagesName,
                );

            }else{

	        	  $data = array(
	                    'jbsj' => date('Y-m-d H:i:s',time()),
	                    'jbr' => $input['jbr'],
	                    'lxdh' => $input['lxdh'],
	                    
	                    'jbnr' => $input['jbnr'],
	                    'unit_id' =>  Auth::guard('admin')->user()->unit_id,
	                    'created_at' => date('Y-m-d H:i:s',time()),
	                );
            }

            $re = DB::table('yjfk')->insert($data);
            if ($re) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }



     	}

     	$map['id'] = Auth::guard('admin')->user()->unit_id;
        $units = DB::table('units')->where($map)->first();
        return view('admin.yjfk.add',['units'=>$units]);

    }



       //修改
    public function edit($id,Request $request){
        if($request->isMethod('post')){
            $this->validate($request, [
                'cldz' => 'required',
                'cldw' => 'required',
                'clry' => 'required',
                'cldh' => 'required',
                'clqk' => 'required',
            ],$this->messages('edit'));

            $input = $request->all();
            $data = Input::except('_token');
                 
            $datas = array(
                'clsj' => date('Y-m-d H:i:s',time()),
                'cldz' => $input['cldz'],
                'cldw' => $input['cldw'],
                'clry' => $input['clry'],
                'cldh' => $input['cldh'],
                'clqk' => $input['clqk'],
                'police_id' =>  Auth::guard('admin')->user()->id,
                'update_at' => date('Y-m-d H:i:s',time()),
                'police_cl' => '1',
            );

            $upjq = DB::table('yjfk')->where('yj_id',$id)->update($datas);

            if ($upjq) {
                 return ['status'=>1,'msg'=>'保存成功'];
            }else{
                 return ['status'=>0,'msg'=>'保存失败'];
            }

        }


       // 获取相关信息
        $re = DB::table('yjfk')->where('yj_id',$id)->first();
        if (Auth::guard('admin')->user()->role_id == 22) {
            $map['id'] = Auth::guard('admin')->user()->unit_id;
        }else{
             $map['id'] = $re->unit_id;
        }
        $units = DB::table('units')->where($map)->first();
        return view('admin.yjfk.edit', ['id'=>$id,'re'=>$re,'units'=>$units]);

    }



    //民警获取企业的人员审核
    public function police($id,Request $request){
        //获取该企业的上报

        if($request->rows) {

            $userid['unit_id'] = $id;
            $res = DB::table('yjfk')->where($userid)->orderBy('yj_id', 'DESC')->paginate($request->rows);
            $list = $res->items();

            foreach ($list as $k=>$v){
                if($v->police_cl == 0){
                     $list[$k]->police_cl = '未处理';
                }else{
                     $list[$k]->police_cl = '已处理';
                }

               

            }

            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);

        }else{

            return view('admin.yjfk.police',['id'=>$id]);
        }
        
    }

     //删除警
    public function del($id,Request $request){

        $re = db::table('yjfk')->where('yj_id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
    }



}